<template>
  <div class="merchant-product-page">
    <el-card class="header-card">
      <el-menu
        :default-active="activeTab"
        mode="horizontal"
        @select="handleTabChange"
      >
        <el-menu-item index="add">新增商品</el-menu-item>
        <el-menu-item index="pass">审核通过</el-menu-item>
        <el-menu-item index="fail">审核失败</el-menu-item>
        <el-menu-item index="unchecked">等待审核</el-menu-item>
      </el-menu>
    </el-card>

    <div class="content">
      <!-- 新增商品 -->
      <el-form
        v-if="activeTab === 'add'"
        :model="newProduct"
        label-width="100px"
        class="product-form"
      >
        <el-form-item label="商品名称">
          <el-input v-model="newProduct.productName" />
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="newProduct.productBrand" />
        </el-form-item>
        <el-form-item label="类型">
          <el-input v-model="newProduct.productType" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input-number v-model="newProduct.productPrice" :min="0" />
        </el-form-item>
        <el-form-item label="颜色">
          <el-input v-model="newProduct.productColor" />
        </el-form-item>
        <el-form-item label="尺寸">
          <el-input v-model="newProduct.productSize" />
        </el-form-item>
        <el-form-item label="图片地址">
          <el-input v-model="newProduct.productMainImg" />
        </el-form-item>
        <el-form-item label="库存">
          <el-input-number v-model="newProduct.productCount" :min="0" />
        </el-form-item>
        <!-- <el-form-item label="评分">
          <el-input-number v-model="newProduct.productPoint" :min="0" :max="10" :step="0.1" />
        </el-form-item> -->
        <el-form-item label="产地">
          <el-input v-model="newProduct.productArea" />
        </el-form-item>
        <el-button type="primary" @click="submitProduct">提交</el-button>
      </el-form>

      <!-- 商品列表展示 -->
      <div v-else class="product-list">
        <el-table :data="productList" style="width: 100%">
          <el-table-column prop="productName" label="名称" />
          <el-table-column prop="productBrand" label="品牌" />
          <el-table-column prop="productType" label="类型" />
          <el-table-column prop="productPrice" label="价格" />
          <el-table-column prop="productCount" label="库存" />
          <el-table-column prop="productStatus" label="状态">
            <template #default="{ row }">
              <el-tag :type="statusTagType(row.productStatus)">
                {{ statusText(row.productStatus) }}
              </el-tag>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { productcheckAPI } from '@/http'

const activeTab = ref('add')
const productList = ref<any[]>([])

// 新增商品表单
const newProduct = reactive({
  productName: '',
  productBrand: '',
  productType: '',
  productPrice: 0,
  productColor: '',
  productSize: '',
  productMainImg: '',
  productCount: 0,
  productPoint: 10,
  productArea: '',
  productSold: 0,
  yy: 1008,
  productVersion: 0,
  productStatus: 0,
})

// 提交新增商品
const submitProduct = async () => {
  try {
    await productcheckAPI.addProduct({ ...newProduct })
    ElMessage.success('新增商品成功')
    Object.assign(newProduct, {
      productName: '',
      productBrand: '',
      productType: '',
      productPrice: 0,
      productColor: '',
      productSize: '',
      productMainImg: '',
      productCount: 0,
      productPoint: 10,
      productArea: '',
      productSold: 0,
      yy: 1008,
      productVersion: 0,
      productStatus: 0,
    })
  } catch (e) {
    ElMessage.error('新增商品失败')
  }
}

// 切换导航
const handleTabChange = (tab: string) => {
  activeTab.value = tab
  if (tab !== 'add') fetchProductList()
}

// 获取商品列表
const fetchProductList = async () => {
  try {
    let res
    if (activeTab.value === 'pass') {
      res = await productcheckAPI.getCheckedProductList(1)
    } else if (activeTab.value === 'fail') {
      res = await productcheckAPI.getUnCheckedProductList(1)
    } else if (activeTab.value === 'unchecked') {
      res = await productcheckAPI.getProductList(1)
    }
    productList.value = Array.isArray(res?.data) ? res.data : []
  } catch (e) {
    ElMessage.error('获取商品数据失败')
  }
}

const statusText = (status: number) => {
  switch (status) {
    case 0:
      return '等待审核'
    case 1:
      return '审核通过'
    case 2:
      return '审核失败'
    default:
      return '未知状态'
  }
}

const statusTagType = (status: number) => {
  switch (status) {
    case 0:
      return 'warning'
    case 1:
      return 'success'
    case 2:
      return 'danger'
    default:
      return ''
  }
}

onMounted(() => {
  if (activeTab.value !== 'add') fetchProductList()
})
</script>

<style scoped>
.merchant-product-page {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.product-form {
  max-width: 600px;
  background-color: #fff;
  padding: 24px;
  border-radius: 8px;
}

.product-list {
  background: #fff;
  padding: 16px;
  border-radius: 8px;
}
</style>
